package com.brkj.util;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLDecoder;

import com.google.gson.Gson;

public class JsonUtil {

	public static <T> T fromJson(String json, Class<T> classOfT) {
		Gson gson = new Gson();
		
		T result = null;
		try {
			result = gson.fromJson(json, classOfT);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		//将类中的字符串  解码
		if((result!=null) && (result.getClass() == classOfT)){
			decodeClass(result);
		}
		
		return result;
	}
	
//	DS_Course
//	Gson gson = new Gson();
//	Type listType = new TypeToken<List<Person>>(){}.getType();
//	List<Person> persons = gson.fromJson(json, listType);
	
	
	private static void  decodeClass(Object obj ){
		if(obj == null){
			return;
		}
		
		Class classOfT = obj.getClass();
		Field[] fields = ((Class) classOfT).getDeclaredFields();
		
		if(fields!=null && fields.length>0){
			for(Field field : fields){

		        String fieldName = field.getName();
				try {
					if(field.getType().isPrimitive()){
						
						continue;
						
					}else if(field.getType() ==  Class.forName("java.lang.String")){
			            String firstLetter = fieldName.substring(0, 1).toUpperCase();
			            // 获得和属性对应的getXXX()方法的名字
			            String getMethodName = "get" + firstLetter + fieldName.substring(1);
			            // 获得和属性对应的setXXX()方法的名字
			            String setMethodName = "set" + firstLetter + fieldName.substring(1);
			 
			            // 获得和属性对应的getXXX()方法
			            Method getMethod = ((Class) classOfT).getMethod(getMethodName, new Class[]{});
			            // 获得和属性对应的setXXX()方法
			            Method setMethod = ((Class) classOfT).getMethod(setMethodName, new Class[]{field.getType()});
			 
			            // 调用原对象的getXXX()方法
			            Object value = getMethod.invoke(obj, new Object[]{});
			         
			            if((value != null)  &&(!value.equals(""))){
				            String decodeStr = URLDecoder.decode((String)value, "UTF-8");
				            // 调用拷贝对象的setXXX()方法
				            setMethod.invoke(obj, new Object[]{decodeStr});
			            }

			            
					}else if(field.getType().getPackage().getName().equals("com.brkj.model")){
							
						decodeClass(field.get(obj));
						
					}

				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		        

			}
		}
	}
}
